// pages/diet/MealsetDetail/MealsetDetail.js
import echarts from '../../../ec-canvas/echarts'
const app = getApp();
var recipeChart = [];

function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  var option = {
    color: ["#36648b", "#2eaa8e", "#ffa500"],
    title: {
      text: '食物成分分析',
      left: 'center',
      textStyle: {
        fontWeight: 'bold',
      },
      subtext: '总热量 ' + recipeChart.energy + ' 千卡'
    },
    tooltip: {
      trigger: 'item',
      formatter: "{a}\n{b}: {c} ({d}%)"
    },
    legend: {
      orient: 'vertical',
      x: 'left',
      data: ['碳水化合物', '脂肪', '蛋白质'],
      textStyle: {
        fontSize: '12',
      }
    },

    series: [
      {
        name: '主要成分',
        type: 'pie',
        radius: '50%',
        center: ['50%', '60%'],
        label: {
          normal: {
            show: true,
            position: 'outside',
            formatter: '{b}',
            fontSize: 13
          },
          emphasis: {
            show: true,
            textStyle: {
              fontSize: '13'
            }
          }
        },
        data: [
          { value: recipeChart.carbohydrate, name: "碳水化合物" },
          { value: recipeChart.fat, name: "脂肪" },
          { value: recipeChart.protein, name: "蛋白质" }
        ]
      }
    ]
  }
  chart.setOption(option);
  return chart;
}


Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      onInit: initChart
    },
    calorieMeal: {},
    elementInfo: ''
  },
  /**
   * 生命周期函数--监听页面加载
   * 
   *
   */
  onLoad: function (options) {
    // console.log(options.mid)
    this.getMealSetDetail(options.mid)
  },

  getMealSetDetail(id) {
    let urlBase = app.globalData.urlBase
    let _self = this;
    wx.request({
      url: urlBase + '/wx/mealsetAnalysis',
      method: 'GET',
      data: {
        id: id
      },
      success(res) {
        console.log(res.data);
        _self.setData({
          calorieMeal: res.data.calorieMeal,
          elementInfo: res.data.elementInfo,
        })
        recipeChart = res.data.calorieMeal
      }
    })
  },

  
  showModal(e) {
    // console.log(e)
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
})